#! /bin/bash
NAMESPACE=workerman
PROJECT_NAME=workerman-gateway
CONFIG_FILE="gateway/values.yaml"
ENV=""
if [ "$3" != "" ];then
    if [  ! -e $3 ];then
        echo "env file $3 not exists!"
        exit 2
    else
        ENV="--kubeconfig $3"
    fi
fi
function getConfigFile(){
    if [ "$1" != "" ];then
        CONFIG_FILE="gateway/values-$1.yaml"
    fi

    echo $CONFIG_FILE
}

case "$1" in
install)
    #getConfigFile $2
    CONFIG_FILE=$(getConfigFile $2)
    if [  ! -e $CONFIG_FILE ];then
	echo "config file $CONFIG_FILE not exists!"
        exit 2
    fi

    helm install $PROJECT_NAME gateway/ --namespace $NAMESPACE -f $CONFIG_FILE $ENV
    ;;
upgrade)
    CONFIG_FILE=$(getConfigFile $2)
    if [  ! -e $CONFIG_FILE ];then
        echo "config file $CONFIG_FILE not exists!"
        exit 2
    fi
    helm upgrade $PROJECT_NAME gateway/ --namespace $NAMESPACE -f $CONFIG_FILE $ENV
    ;;
delete)
    helm uninstall $PROJECT_NAME --namespace $NAMESPACE $ENV
    ;;
*)
    echo $"Usage: $0 {install|upgrade|delete} {values_file_optional} {env_file_optional}"
    exit 2
esac
